package com.qidian.k12.service.impl;

import com.qidian.k12.dto.PageInfo;
import com.qidian.k12.mapper.ScoreItemMapper;
import com.qidian.k12.mapper.StudentMapper;
import com.qidian.k12.mapper.TemplateMapper;
import com.qidian.k12.pojo.ScoreItem;
import com.qidian.k12.pojo.Template;
import com.qidian.k12.pojo.TemplateItem;
import com.qidian.k12.pojo.TestScore;
import com.qidian.k12.mapper.TestScoreMapper;
import com.qidian.k12.service.TestScoreService;
import com.qidian.k12.util.K12Result;
import com.qidian.k12.vo.TestScoreSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.*;

/**
 * (TestScore)表服务实现类
 *
 * @author makejava
 * @since 2021-10-30 09:05:08
 */
@Service("testScoreService")
public class TestScoreServiceImpl implements TestScoreService {
    @Resource
    private TestScoreMapper testScoreMapper;
    @Resource
    private ScoreItemMapper scoreItemMapper;
    @Resource
    private TemplateMapper templateMapper;
    @Resource
    private StudentMapper studentMapper;

    public K12Result queryDetail(long scoreId){
        K12Result result = K12Result.ok();
        // 准备一个map
        Map map = new HashMap();
        // 查询成绩详情
        TestScore testScore = testScoreMapper.queryById(scoreId);
        if(testScore!=null){
            // 将成绩详情设置到map中
            map.put("testScore",testScore);
            //查询成绩项目列表
            List<ScoreItem> scoreItems = scoreItemMapper.queryByScoreId(scoreId);
            map.put("scoreItems",scoreItems);
            // 查询学生信息
            map.put("student",studentMapper.queryById(testScore.getStudentNo()));
            // 查询模版信息
            map.put("template",templateMapper.queryById(testScore.getTemplateId()));
            // 查询各项目的平均分
            List<Map> scoreAvgs = new ArrayList<>();
            for (TemplateItem item : ((Template)(map.get("template"))).getTemplateItems()){
                float avg = scoreItemMapper.queryAvg(item.getItemid());
                // 准备map   {templateItemId:183,avg:85}
                Map scoreAvg = new HashMap();
                scoreAvg.put("templateItemId",item.getItemid());
                scoreAvg.put("avg",avg);
                scoreAvgs.add(scoreAvg);
            }
            map.put("scoreAvgs",scoreAvgs);
            //将map放result
            result.setData(map);
        }
        return result;
    };

    /**
     * 分页查询
     * @param map
     * {page,row,scoreName,templateId,studentNo,studentName,timeStart,timeEnd,scoreStart,ScoreEnd,score,scoreStatus}
     * @return K12Result 中包裹一个pageInfo
     */
    public K12Result queryByPager(Map map){
        K12Result result = K12Result.ok();
        PageInfo<TestScore> pageInfo = new PageInfo<>();
        int page = new Integer(map.get("page").toString());
        int rows = new Integer(map.get("row").toString());
        pageInfo.setPage(page);
        pageInfo.setRow(rows);
        // 查询总条数
        int total = testScoreMapper.total(map);
        pageInfo.setTotal(total);
        int maxPageNum = (int)(Math.ceil(total * 1.0 / rows));
        pageInfo.setMaxPageNun(maxPageNum);
        // 计算分页栏中的开始和结束位置
        //计算页码的开始位置和结束位置
        int pageStart = 1;
        int pageEnd = 10;
        if(page - 4 > 1){
            pageStart = page - 4;
        }
        if(page + 5 > maxPageNum){
            pageEnd = (int) maxPageNum;
        }
        pageInfo.setPageStart(pageStart);
        pageInfo.setPageEnd(pageEnd);
        //查询数据
        pageInfo.setData(testScoreMapper.queryByPage(map));
        result.setData(pageInfo);
        return result;
    }

    @Transactional
    public K12Result save(TestScoreSaveVo vo){
        // 保存testScore
        TestScore testScore = vo.getTestScore();
        // 补全数据
        Date time = new Date();
        testScore.setCreateTime(time);
        testScore.setModifyTime(time);
        testScore.setScoreStatus(1);
        if(testScoreMapper.insert(testScore)==1){
            long scoreId = testScore.getScoreId();
            //循环的保存scoreItem
            List<Map> mapList = vo.getScoreItems();
            // 类型转换
            List<ScoreItem> scoreItems = new ArrayList<>();
            for(Map map : mapList){
                ScoreItem scoreItem = new ScoreItem();
                scoreItem.setScoreId(scoreId);
                scoreItem.setScore((long)(Double.parseDouble(map.get("score").toString())*100));
                scoreItem.setTemplateItemId(Long.parseLong(map.get("itemid").toString()));
                scoreItems.add(scoreItem);
            }
            // 保存scoreItems中的所有数据
            scoreItemMapper.insertBatch(scoreItems);
        }
        return K12Result.ok();
    }
    /**
     * 通过ID查询单条数据
     *
     * @param scoreId 主键
     * @return 实例对象
     */
    @Override
    public TestScore queryById(Long scoreId) {
        return this.testScoreMapper.queryById(scoreId);
    }


    /**
     * 新增数据
     *
     * @param testScore 实例对象
     * @return 实例对象
     */
    @Override
    public TestScore insert(TestScore testScore) {
        this.testScoreMapper.insert(testScore);
        return testScore;
    }

    /**
     * 修改数据
     *
     * @param testScore 实例对象
     * @return 实例对象
     */
    @Override
    public TestScore update(TestScore testScore) {
        this.testScoreMapper.update(testScore);
        return this.queryById(testScore.getScoreId());
    }

    /**
     * 通过主键删除数据
     *
     * @param scoreId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long scoreId) {
        return this.testScoreMapper.deleteById(scoreId) > 0;
    }
}
